package cn.nawang.ebeim.api.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.TbStorageStrategy;
import cn.nawang.ebeim.api.repository.TbStorageStrategyRepository;
import cn.nawang.ebeim.api.repository.impl.TbStorageStrategyRepositoryImpl;
import cn.nawang.ebeim.api.service.TbStorageStrategyService;
import cn.nawang.ebeim.api.utils.ValidateUtil;

/**
 * @ClassName: TbStorageStrategyServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Oct 12, 2015 11:39:41 AM
 * @version v1.0
 */
@Service("tbStorageStrategyService")
public class TbStorageStrategyServiceImpl implements TbStorageStrategyService{

  @Autowired
  private TbStorageStrategyRepositoryImpl tbStorageStrategyRepositoryImpl;
  
  @Autowired
  private TbStorageStrategyRepository tbStorageStrategyRepository;
  
  @Override
  public List<TbStorageStrategy> findByCustomerId(String customerId) {
    return tbStorageStrategyRepositoryImpl.findByCustomerId(customerId);
  }

  @Override
  public boolean findByfindByStorageCloudId(String storageCloudId) {
    TbStorageStrategy tbStorageStrategy=tbStorageStrategyRepositoryImpl.findByStorageCloudId(storageCloudId);
    if(ValidateUtil.isNull(tbStorageStrategy)){
      return false;
    }
    return true;
  }

  @Override
  public boolean add(TbStorageStrategy tbStorageStrategy) {
    try {
      tbStorageStrategy.setId("TSS-" + new Date().getTime() + "-" + new Random().nextInt(1000));
      tbStorageStrategyRepository.save(tbStorageStrategy);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public PageList findPageList(Pages page) {
    return tbStorageStrategyRepositoryImpl.findAllByPaging(page);
  }

  @Override
  public TbStorageStrategy findOne(String id) {
    return tbStorageStrategyRepository.findOne(id);
  }

  @Override
  public boolean delete(String[] ids) {
    return tbStorageStrategyRepositoryImpl.delete(ids);
  }

  @Override
  public boolean update(TbStorageStrategy tbStorageStrategy) {
    try {
      tbStorageStrategyRepository.save(tbStorageStrategy);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public TbStorageStrategy findByIsPrimary(String customerId, int isPrimary) {
    return tbStorageStrategyRepositoryImpl.findByIsPrimary(customerId, isPrimary);
  }

  @Override
  public TbStorageStrategy findByStorageCloudIdAndCustomerId(String customerId,
      String storageCloudId) {
    return tbStorageStrategyRepositoryImpl.findByStorageCloudIdAndCustomerId(customerId, storageCloudId);
  }

}
